package com.hxzon.aixzsecretary.mail;

import java.awt.AWTEvent;
import java.awt.Button;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

/**
 * 
 * @author hxzon
 */
public class SendMailFrame extends JFrame {
	JPanel contentPane;
	Label labelFrom = new Label();
	TextField textFrom = new TextField("");// 初始默认
	Label labelPassword = new Label();
	JPasswordField textPassword = new JPasswordField();
	Label labelTo = new Label();
	TextField textTo = new TextField("");
	Label labelSubject = new Label();
	TextField textSubject = new TextField("");
	TextArea textBody = new TextArea("");
	Label labelAffix = new Label();
	TextField textAffix = new TextField();
	Button buttonSend = new Button();
	Button buttonAffix = new Button();

	/** Creates a new instance of AixzSendMailFrame */
	public SendMailFrame() {
		// 界面初始化并显示
		contentPane = (JPanel) this.getContentPane();
		contentPane.setLayout(null);

		labelFrom.setText("发件人:"); // x,y,width,height
		labelFrom.setBounds(25, 50, 50, 22);
		textFrom.setBounds(80, 50, 100, 22);

		labelPassword.setText("密码:");
		labelPassword.setBounds(185, 50, 37, 22);
		textPassword.setBounds(240, 50, 100, 22);

		labelTo.setText("收件人:");
		labelTo.setBounds(25, 85, 37, 22);
		textTo.setBounds(80, 85, 100, 22);

		labelAffix.setText("附件:");
		labelAffix.setBounds(25, 120, 37, 22);
		textAffix.setBounds(80, 120, 280, 22);

		// 选择附件按钮
		buttonAffix.setLabel("添加附件");
		buttonAffix.setBounds(365, 120, 70, 22);
		buttonAffix.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSetAffix(e);
			}
		});

		labelSubject.setText("主题:");
		labelSubject.setBounds(25, 155, 37, 22);
		textSubject.setBounds(80, 155, 280, 22);

		textBody.setBounds(25, 190, 420, 200);

		// 发送按钮
		buttonSend.setLabel("发送");
		buttonSend.setBounds(375, 447, 70, 22);
		buttonSend.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnSendMail(e);
			}
		});
		buttonSend.setEnabled(true);

		// 添加各个控件

		contentPane.add(labelFrom, null);
		contentPane.add(textFrom, null);

		contentPane.add(labelPassword, null);
		contentPane.add(textPassword, null);

		contentPane.add(labelTo, null);
		contentPane.add(textTo, null);

		contentPane.add(labelSubject, null);
		contentPane.add(textSubject, null);

		contentPane.add(labelAffix, null);
		contentPane.add(textAffix, null);

		contentPane.add(textBody, null);
		contentPane.add(buttonSend, null);
		contentPane.add(buttonAffix, null);

		enableEvents(AWTEvent.WINDOW_EVENT_MASK);

		// contentPane.setBackground(Color.pink);
		this.setSize(480, 520);// new Dimension(480, 520)
		this.setResizable(false);
		this.setTitle("xZSendMailFrame");
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}// end xZSendMailFrame

	public void btnSetAffix(ActionEvent e) {
		JFileChooser affixChooser = new JFileChooser();// 选择附件
		// ;//int returnVal=
		if (affixChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
			textAffix.setText(affixChooser.getSelectedFile().getAbsolutePath());
		System.out.println(textAffix.getText());
	}

	public void btnSendMail(ActionEvent e) {
		try {
			SendMail cn = new SendMail();
			// 设置发件人地址、收件人地址和邮件标题
			cn.setHeader(textFrom.getText(), textTo.getText(), textSubject
					.getText());
			// 设置要发送附件的位置和标题
			cn.setAffix(textAffix.getText(), textAffix.getText());
			// 设置smtp服务器以及邮箱的帐号和密码
			String[] split = textFrom.getText().split("@");
			String name = split[0];// 得到@之前的邮箱用户名
			String host = new String("smtp." + split[1]);
			String password = String.valueOf(textPassword.getPassword());
			cn.setUser(host, name, password);
			cn.setContentText(textBody.getText());
			cn.send();

			JOptionPane msg = new JOptionPane();// 提示成功对话框
			JOptionPane.showMessageDialog(SendMailFrame.this,
					"mail successful", "mail successful", 1);
		} catch (Exception ex) {
			JOptionPane msg = new JOptionPane();// 提示失败对话框
			JOptionPane.showMessageDialog(SendMailFrame.this, "mail fail",
					"mail fail", 1);
			System.out.println("An error about:" + ex.getMessage());
		}
	}

	public static void main(String args[]) {
		new SendMailFrame();
	}
}
